import { listSkuClassByLanguage } from '@/api/platform/goodsClassDict';
import { handleTree } from "@/utils/ruoyi";

export default () => {

  /**是否缓存分类列表 */
  const isCacheSkuClassList = JSON.parse(localStorage.getItem('isCacheSkuClassList')) || false
  const skuClassList = JSON.parse(localStorage.getItem('skuClassList')) || []

  return new Promise(async (resolve, reject) => {
    if (isCacheSkuClassList) {
      if (skuClassList.length == 0) {
        const { data, menuOptions } = await getList()
        localStorage.setItem('skuClassList', JSON.stringify(data))
        resolve({ data, menuOptions })
      } else {
        resolve({
          data: skuClassList,
          menuOptions: handleTree(skuClassList, 'id')
        })
      }
    } else {
      const { data, menuOptions } = await getList()
      // 不启用缓存 也存储下来方便启用直接使用了
      localStorage.setItem('skuClassList', JSON.stringify(data))
      resolve({ data, menuOptions })
    }
  })
}


const getList = async () => {
  const { data } = await listSkuClassByLanguage()
  return {
    data,
    menuOptions: handleTree(data, 'id')
  }
}